<template>
  <div class="box">
    <el-input class="!w-72" placeholder="请输入账号" v-model="user" clearable :prefix-icon="User">
    </el-input>
    <el-input
      class="!w-72 !mt-4"
      placeholder="请输入密码"
      v-model="password"
      show-password
      clearable
      :prefix-icon="Key"
    ></el-input>
    <el-select class="!w-72 !mt-4" v-model="id" placeholder="请选择">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
      </el-option>
    </el-select>
    <el-button class="!w-72 !mt-4" type="primary" :icon="Promotion" @click="logn"
      >登录</el-button
    >
  </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import lognFun from "@/modules/logn";
import { User, Key,Promotion } from "@element-plus/icons-vue";

let router = useRouter();
onMounted(() => {
  document.body.classList.add("logn");
});
onUnmounted(() => {
  document.body.classList.remove("logn");
});
//账号密码身份
let user = ref("");
let password = ref("");
let id = ref("tb_student");
function logn() {
  if (user.value == "" || password.value == "") {
    ElMessage({
      message: "请将信息填写完整",
      type: "error",
    });
  } else {
    lognFun({
      col: id.value,
      id: user.value,
      password: password.value,
    }).then(res => {
      if (res.res == false) {
        ElMessage({
          message: "账号或密码错误",
          type: "error",
        });
      } else {
        localStorage.type = id.value; //身份
        localStorage.id = user.value;
        localStorage.user = res.user; //账号
        localStorage[res.id] = user.value;
        localStorage.name = res.name; // 姓名
        localStorage.is_class_teacher = res.is_class_teacher; // 判断是否是班主任
        
        console.log(res.is_class_teacher);

        if(res.is_class_teacher == 1){
          localStorage.major_id =  res.major_id;
          localStorage.grade = res.grade;
          localStorage.class_name = res.class_name;
        }
        let go = 'hme'
        router.replace({ path: `/${go}`, query: { user: user.value } });
      }
    });
  }
}
let options = ref([
  {
    label: "学生",
    value: "tb_student",
  },
  {
    label: "教师",
    value: "tb_teacher",
  },
  {
    label: "管理员",
    value: "admin",
  },
]);
</script>
<style scoped lang="scss">
.box {
  text-align: center;
  width: 400px;
  background-color: white;
  margin: 0px auto;
  top: calc((100vh - 310px) / 2);
  position: relative;
  border-radius: 10px;
  padding-top: 30px;
  padding-bottom: 30px;
}
</style>
